package com.leaves.sys.modular.timer.tasks;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.leaves.core.annotion.JobLog;
import com.leaves.core.timer.TimerTaskRunner;
import com.leaves.sys.modular.file.entity.SysFileInfo;
import com.leaves.sys.modular.file.param.SysFileInfoParam;
import com.leaves.sys.modular.file.service.SysFileInfoService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import java.util.List;

/**
 * @Author Chenweiwei
 * @Date 2021/9/17 9:43
 * @Version 1.0
 */
@Component
public class FileCleanTask implements TimerTaskRunner {

    private static final Log log = Log.get();
    @Resource
    private SysFileInfoService sysFileInfoService;

   // 定时清理垃圾文件
    @Override
    @JobLog(title = "定时清理垃圾文件")
    public void action() {

        QueryWrapper<SysFileInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().isNull(SysFileInfo::getBusinessIdentification).or().eq(SysFileInfo::getBusinessIdentification,"");
        List<SysFileInfo> fileInfoList= sysFileInfoService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(fileInfoList)){
            for (SysFileInfo sysFileInfo : fileInfoList) {
                SysFileInfoParam sysFileInfoParam = new SysFileInfoParam();
                sysFileInfoParam.setId(sysFileInfo.getId());
                sysFileInfoService.delete(sysFileInfoParam);
            }

            log.info("已删除{}个垃圾文集及记录信息",fileInfoList.size());
        }

    }
}
